<?php

class banners extends Module {
	
	public function __construct () {
		parent::__construct (__CLASS__);		
	}
	
	public function __default () {
		$typesController = TypesController::getInstance ();
		
		$sel = new Selection;
		$sel->addType ($typesController->getTypeByName ('banner'));
		$sel->setOrder ('id', false);
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('banners/bannersList');
		
		if (getRequest ('created','bool')) {
			$response->addObject (new SystemMessage (lang('admin_banner_created',__CLASS__)));
		}
		if (getRequest ('saved','bool')) {
			$response->addObject (new SystemMessage (lang('admin_banner_saved',__CLASS__)));
		}
		if (getRequest ('deleted','bool')) {
			$response->addObject (new SystemMessage (lang('admin_banner_deleted',__CLASS__)));
		}
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		// Страницы
		$sel = new Selection;
		$sel->addType ($typesController->getTypeByName ('banners_page_type'));
		$sel->addSelectedProperty ('title');
		
		$res = $sel->getList ();
		
		$aPagesTypes = array ();
		
		foreach ($res as $row) {
			$aPagesTypes[]['type:id='.$row[0]] = $row['title'];
		}
		
		$response->addObject (new Array2XML ('ptypes', $aPagesTypes));
		
		// Позиции баннера на странице
		$sel = new Selection;
		$sel->addType ($typesController->getTypeByName ('banner_position'));
		$sel->addSelectedProperty ('title');
		
		$res = $sel->getList ();
		
		$aPositions = array ();
		
		foreach ($res as $row) {
			$aPositions[]['position:id='.$row[0]] = $row['title'];
		}
		
		$response->addObject (new Array2XML ('positions', $aPositions));
		
		return $response;
	}
	
	public function add_banner () {
		return $this->formBanner ();
	}
	
	public function edit_banner () {
		return $this->formBanner (getRequest ('param0','int'));
	}
	
	public function delete_banner () {
		$this->expectSecureGET ();
				
		if (!$oBanner = ObjectsController::getInstance()->getObject (TypesController::getInstance()->getTypeByName ('banner'), getRequest ('param0','int'))) {
			throw new NotFound;
		}
		
		$oBanner->delete ();
		
		redirect (HTTP_HOST . '/admin/banners?deleted');
	}
	
	public function ajax_load_positions () {
		$oTypePos = TypesController::getInstance()->getTypeByName ('banner_position');
		
		$sel = new Selection;
		$sel->addType ($oTypePos);
		$sel->setOrder ('title');
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('banners/positionsList');
		
		if (getRequest ('created','bool')) {
			$response->addObject (new SystemMessage (lang('admin_position_created',__CLASS__)));
		}
		if (getRequest ('saved','bool')) {
			$response->addObject (new SystemMessage (lang('admin_position_saved',__CLASS__)));
		}
		if (getRequest ('deleted','bool')) {
			$response->addObject (new SystemMessage (lang('admin_position_deleted',__CLASS__)));
		}
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	public function ajax_add_position_form () {
		$this->setResponseFormat ('xml');
		
		return $this->formPosition ();
	}
	
	public function ajax_edit_position_form () {
		$this->setResponseFormat ('xml');
		
		return $this->formPosition (getRequest ('param0','int'));
	}
	
	public function ajax_delete_position () {
		$this->expectSecureGET ();
		
		if (!$object = ObjectsController::getInstance()->getObject (TypesController::getInstance()->getTypeByName ('banner_position'), getRequest ('param0','int'))) {
			$this->flush ((string) new Array2XML ('status', array(0)), 'text/xml');
		}
		
		$object->delete ();
		
		$this->flush ((string) new Array2XML ('status', array(1)), 'text/xml');
	}
	
	public function ajax_position_save () {
		$this->setResponseFormat ('xml');
		
		$oTypePos = TypesController::getInstance()->getTypeByName ('banner_position');
		
		$id = getRequest ('id','bool') ? getRequest ('id','int') : 'new';
		
		if ($id != 'new') {
			if (!$oPos = ObjectsController::getInstance()->getObject ($oTypePos, $id)) {
				throw new NotFound;
			}
		} else {
			$oPos = new Object ($oTypePos);
		}
		
		$oPos->setValues ($this->getRequestData ($id, $oTypePos));
		
		$aErrors = $this->getValidateErrors ($oPos);
		
		if (!isset ($aErrors['alias'])) {
			$sel = new Selection;
			$sel->addType ($oTypePos);
			$sel->addFilter ('alias', $oPos->getValue ('alias'));
			if ($id != 'new') {
				$sel->addFilter ('id', $id, '!=');
			}
			
			if ($sel->getTotalCount () != 0) {
				$aErrors['alias'] = lang ('admin_error_position_alias_exists',__CLASS__);
			}
		}
		
		if (count ($aErrors) != 0) {
			$aResponseErrors = array ();
			
			foreach ($aErrors as $fieldName => $error) {
				$aResponseErrors[]['field:name='.$fieldName] = $error;
			}
			
			$response = new ModuleResponse;
			$response->addObject (new Array2XML ('status', array(0)));
			$response->addObject (new Array2XML ('errors', $aResponseErrors));
			
			return $response;
		}
		
		if ($id == 'new') {
			$oPos->createNew ();
		} else {
			$oPos->save ();
		}
		
		$response = new ModuleResponse;
		$response->addObject (new Array2XML ('status', array(1)));
		
		return $response;
	}
	
	public function ajax_load_types () {
		$oTypePos = TypesController::getInstance()->getTypeByName ('banners_page_type');
		
		$sel = new Selection;
		$sel->addType ($oTypePos);
		$sel->setOrder ('title');
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('banners/typesList');
		
		if (getRequest ('created','bool')) {
			$response->addObject (new SystemMessage (lang('admin_type_created',__CLASS__)));
		}
		if (getRequest ('saved','bool')) {
			$response->addObject (new SystemMessage (lang('admin_type_saved',__CLASS__)));
		}
		if (getRequest ('deleted','bool')) {
			$response->addObject (new SystemMessage (lang('admin_type_deleted',__CLASS__)));
		}
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	public function ajax_add_type_form () {
		$this->setResponseFormat ('xml');
		
		return $this->formType ();
	}
	
	public function ajax_edit_type_form () {
		$this->setResponseFormat ('xml');
		
		return $this->formType (getRequest ('param0','int'));
	}
	
	public function ajax_delete_type () {
		$this->expectSecureGET ();
		
		if (!$object = ObjectsController::getInstance()->getObject (TypesController::getInstance()->getTypeByName ('banners_page_type'), getRequest ('param0','int'))) {
			$this->flush ((string) new Array2XML ('status', array(0)), 'text/xml');
		}
		
		$object->delete ();
		
		$this->flush ((string) new Array2XML ('status', array(1)), 'text/xml');
	}
	
	private function formType ($id = 'new') {
		$oTypePos = TypesController::getInstance()->getTypeByName ('banners_page_type');
		
		$form = new Form ($oTypePos, $id);
		$form->buildFromType ();
		
		$aValues = array ();
		
		if ($id != 'new') {
			if (!$oPos = ObjectsController::getInstance()->getObject ($oTypePos, $id)) {
				throw new NotFound;
			}
			
			$aValues = $oPos->getValues ();
		}
		
		$form->setValues ($aValues);
		
		$response = new ModuleResponse;
		$response->addObject ($form);
		
		return $response;
	}
	
	public function ajax_type_save () {
		$this->setResponseFormat ('xml');
		
		$oType = TypesController::getInstance()->getTypeByName ('banners_page_type');
		
		$id = getRequest ('id','bool') ? getRequest ('id','int') : 'new';
		
		if ($id != 'new') {
			if (!$object = ObjectsController::getInstance()->getObject ($oType, $id)) {
				throw new NotFound;
			}
		} else {
			$object = new Object ($oType);
		}
		
		$object->setValues ($this->getRequestData ($id, $oType));
		
		$aErrors = $this->getValidateErrors ($object);
		
		if (!isset ($aErrors['alias'])) {
			$sel = new Selection;
			$sel->addType ($oType);
			$sel->addFilter ('alias', $object->getValue ('alias'));
			if ($id != 'new') {
				$sel->addFilter ('id', $id, '!=');
			}
			
			if ($sel->getTotalCount () != 0) {
				$aErrors['alias'] = lang ('admin_error_type_alias_exists',__CLASS__);
			}
		}
		
		if (count ($aErrors) != 0) {
			$aResponseErrors = array ();
			
			foreach ($aErrors as $fieldName => $error) {
				$aResponseErrors[]['field:name='.$fieldName] = $error;
			}
			
			$response = new ModuleResponse;
			$response->addObject (new Array2XML ('status', array(0)));
			$response->addObject (new Array2XML ('errors', $aResponseErrors));
			
			return $response;
		}
		
		if ($id == 'new') {
			$object->createNew ();
		} else {
			$object->save ();
		}
		
		$response = new ModuleResponse;
		$response->addObject (new Array2XML ('status', array(1)));
		
		return $response;
	}
	
	private function formPosition ($id = 'new') {
		$oTypePos = TypesController::getInstance()->getTypeByName ('banner_position');
		
		$form = new Form ($oTypePos, $id);
		$form->buildFromType ();
		
		$aValues = array ();
		
		if ($id != 'new') {
			if (!$oPos = ObjectsController::getInstance()->getObject ($oTypePos, $id)) {
				throw new NotFound;
			}
			
			$aValues = $oPos->getValues ();
		}
		
		$form->setValues ($aValues);
		
		$response = new ModuleResponse;
		$response->addObject ($form);
		
		return $response;
	}
	
	private function formBanner ($id = 'new') {		
		$oTypeBanner = TypesController::getInstance()->getTypeByName ('banner');
		
		$form = new Form ($oTypeBanner, $id);
		$form->buildFromType ();
		
		$aValues = array ();
		$aErrors = array ();
		
		if ($id != 'new') {
			if (!$oBanner = ObjectsController::getInstance()->getObject ($oTypeBanner, $id)) {
				throw new NotFound;
			}
			
			$aValues = $oBanner->getValues ();
		}
		
		if (($id == 'new' && getRequest ('param0') == 'save') || ($id != 'new' && getRequest ('param1') == 'save')) {
			$aValues = $this->getRequestData ($id, $oTypeBanner);

			if ($id == 'new') {
				$oBanner = new Object ($oTypeBanner);
				$oBanner->setValues ($aValues);
				
				$aErrors = $this->getValidateErrors ($oBanner);
				
				if (!count ($aErrors)) {
					$oBanner->createNew ();
					redirect (HTTP_HOST . '/admin/banners?created');
				}
			} else {
				$oBanner->setValues ($aValues);
				
				$aErrors = $this->getValidateErrors ($oBanner);
				
				if (!count ($aErrors)) {
					$oBanner->save ();
					redirect (HTTP_HOST . '/admin/banners?saved');
				}
			}
		}
		
		$form->setErrors ($aErrors);
		$form->setValues ($aValues);
		
		$response = new ModuleResponse;
		$response->setTemplate ('banners/formBanner');
		$response->addObject ($form);
		
		return $response;
	}
	
	/*private function getTypesIds () {
		if ($this->aTypesIds !== null) {
			return $this->aTypesIds;
		}
		
		$this->aTypesIds = array ();
		
		$sel = new Selection;
		$sel->addType (TypesController::getInstance()->getTypeByName ('banner_type'));
		$sel->addSelectedProperty ('type_id');
		
		$res = $sel->getList ();
		
		foreach ($res as $row) {
			$this->aTypesIds[] = $row['type_id'];
		}
		
		return $this->aTypesIds;
	}*/
	
}